#pragma once
#include <iostream>
#include <memory>
#include <functional>
#include "InetAddr.hpp"
#include "Log.hpp"
#include "Protocol.hpp"
#include "Connection.hpp"
#include "NetCal.hpp"

using namespace log_ns;


class PackageParse
{
public:
    void Excute(Connection* conn)
    {
        while (true)
        {
            //1. 负责读取
            // ... 下层做了
            
            //能保证读到一个完整的报文吗 : 不能
            //2. 报文解析
            std::string package = Decode(conn->Inbuffer());
            if(package.empty())
                return ;
            //走到这里，一定能读到一个完整的报文
            //3. 请求反序列化
            auto req = Factory::BuidRequestDefault();
            req->Deserialize(package);

            //4. 业务处理
            //auto resp = _process(req);
            auto resp = cal.Calculator(req);

            //5. 应答序列化
            std::string respjson;
            resp->Serialize(&respjson);

            //6. 添加报头
            respjson = Encode(respjson);

            //7. 发送应答
            conn->AddOutBuff(respjson);
            // sockfd->Send(respjson);

            //至少有一个应答
            if(!conn->Outbuffer().empty()   )conn->_handler_sender(conn);
        }
    }

private:
    NetCal cal;
};

